﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using System.Collections.Generic;
using DBAccess;
namespace SD.Database
{

    public class Agreements
    {
        public static Dictionary<int, int> TABonuses()
        {
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            List<int> players = UserFunctions.GetPlayerIDs();
            foreach (int i in players)
                tmp.Add(i, TABonus(i));
            return tmp;
        }
        public static List<int> TAPartners(int UserId)
        {
            List<int> partners = new List<int>();
            DataSet ds = DAL.GetInstance().GetData("Select * From Agreements Where (pId1 = @param0 or pId2 = @param0) and TypeA = 0 and Status = 1", new List<object>() { UserId });
            if (ds.Tables.Count > 0)
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    int from = int.Parse(row[0].ToString());
                    int to = int.Parse(row[1].ToString());
                    if (from != UserId) partners.Add(from);
                    if (to != UserId) partners.Add(to);
                }
            return partners;
        }
        public static int TABonus(int UserId)
        {
            int bonus = 0;
            List<int> partners = TAPartners(UserId);
            foreach (int uId in partners)
            {
                bonus += DAL.GetInstance().GetInt("Select Count(*) From Planets Where OwnerID = @param0 and PopulationLevel >= 10", new List<object>() { uId });
            }
            return bonus;
        }
        public static int TACost(int UserId)
        {
            return 20000;
        }
        public static int NumberOfTA(int UserId)
        {
            return DAL.GetInstance().GetInt("Select Count(*) From Agreements Where (pId1 = @param0 or pId2 = @param0) and TypeA = 0", new List<object>() { UserId });
        }
        public static int NumberOfSA(int UserId)
        {
            return DAL.GetInstance().GetInt("Select Count(*) From Agreements Where (pId1 = @param0 or pId2 = @param0) and TypeA = 1", new List<object>() { UserId });
        }
        public static int NumberOfAgreements(int UserId, int type)
        {
            return DAL.GetInstance().GetInt("Select Count(*) From Agreements Where (pId1 = @param0 or pId2 = @param0) and TypeA = @param1", new List<object>() { UserId, type });
        }
        public static int SACost()
        {
            return 30000;
        }
        public static bool AgreementStatus(int p1, int p2, int type)
        {
            string o = DAL.GetInstance().GetScalar("Select Status From Agreements Where (pId1 = @param0 and pId2 = @param1) and TypeA = @param2", new List<object> { p1, p2, type });
            if (o != null && o.ToString() != string.Empty)
                return bool.Parse(o.ToString());
            else
            {//try the other way
                 o = DAL.GetInstance().GetScalar("Select Status From Agreements Where (pId1 = @param0 and pId2 = @param1) and TypeA = @param2", new List<object> { p2, p1, type });
                if (o != null && o.ToString() != string.Empty)
                    return bool.Parse(o.ToString());
                else
                    return false ;
            }
        }
        public static int Cost(int UserId, int type)
        {
            switch (type)
            {
                case 0: return TACost(UserId);
                case 1: return SACost();
                default: return 0;
            }
        }
        public static bool SendAgreement(int from, int to, int type)
        {
            if (type == 0)
            {
                if (NumberOfTA(from) >= 5 || NumberOfTA(to) >= 5)
                    return false;
            }
            else if (type == 1)
            {
                if (NumberOfSA(from) >= 1 || NumberOfSA(to) >= 1)
                    return false;
            }
            if (from == to)
                return false;
            int cost = Cost(from, type);
            if ((int)UserFunctions.GetPlayerCash(from) < cost)
                return false;
            if (AgreementStatus(from, to, type) != false)
                return false;
            bool insertSucces = false;
            DAL.GetInstance().InsertData("Insert Into Agreements Values (@param0, @param1, 0, @param2)", new List<object>() { from, to, type });
            DAL.GetInstance().UpdateData("Update Players set Cash = Cash - @param0 Where PlayerId = @param1", new List<object>() { cost, from });

            switch (type)
            {
                case 0:
                    StoredProcedures.InsertNews(to, DateTime.Now, "Player <a href=\"PlayerInfo.aspx?pId=" + from.ToString() + "\">" + UserFunctions.PlayerName(from) + "</a> send you trade cooperation offer.<br/> You may accept it on Agreements screen.");
                    break;
                case 1:
                    StoredProcedures.InsertNews(to, DateTime.Now, "Player <a href=\"PlayerInfo.aspx?pId=" + from.ToString() + "\">" + UserFunctions.PlayerName(from) + "</a> send you science cooperation offer.<br/> You may accept it on Agreements screen.");
                    break;
            }
            return true;
        }
        public static bool RejectAgreement(int from, int to, int type)
        {
            DAL.GetInstance().DeleteData("Delete From Agreements Where Status = 0 Where pId1 = @param0 and pId2 = @param1 and TypeA = @param2)", new List<object>() { from, to, type });
            return true;
        }
        public static bool AcceptAgreement(int from, int to, int type)
        {
            int cost = Cost(to, type);
            if ((int)UserFunctions.GetPlayerCash(to) < cost)
                return false;
            bool status = AgreementStatus(from, to, type);
            if (status != false)
                return false;
            else
            {//ready to accept
                bool updateSuccess = false;
                DAL.GetInstance().UpdateData("Update Agreements Set Status = 1 Where pId1 = @param0 and pId2 = @param1 and TypeA = @param2", new List<object>() { from, to, type });
                DAL.GetInstance().UpdateData("Update Players set Cash = Cash - @param0 Where PlayerId = @param1", new List<object>() { to, type });

                StoredProcedures.InsertNews(from, DateTime.Now, "Player <a href=\"PlayerInfo.aspx?pId=" + to.ToString() + "\">" + UserFunctions.PlayerName(to) + "</a> accepted your agreement offer.");
                    return true;
            }
        }
    };
}